package org.jeecg.modules.demo.zhengchai.budget.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.zhengchai.budget.entity.Budget;
import org.jeecg.modules.demo.zhengchai.budget.service.BudgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/budget")
public class BudgetController {

    @Autowired
    private BudgetService budgetService;

    /**
     * 获取预算列表
     * @param page 分页对象
     * @param budget 查询条件
     * @return 分页结果
     */
    @GetMapping("/list")
    public ResponseEntity<Page<Budget>> list(Page<Budget> page, Budget budget) {
        QueryWrapper<Budget> queryWrapper = new QueryWrapper<>(budget);
        Page<Budget> budgetPage = budgetService.page(page, queryWrapper);
        return ResponseEntity.ok(budgetPage);
    }

    /**
     * 添加预算
     * @param budget 预算对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResponseEntity<Boolean> add(@RequestBody Budget budget) {
        boolean result = budgetService.save(budget);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改预算
     * @param budget 预算对象
     * @return 是否修改成功
     */
    @PutMapping("/edit")
    public ResponseEntity<Boolean> edit(@RequestBody Budget budget) {
        boolean result = budgetService.updateById(budget);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除预算
     * @param id 预算ID
     * @return 是否删除成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> delete(@RequestParam("id") Long id) {
        boolean result = budgetService.removeById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据ID查询预算
     * @param id 预算ID
     * @return 预算对象
     */
    @GetMapping("/queryById")
    public ResponseEntity<Budget> queryById(@RequestParam("id") Long id) {
        Budget budget = budgetService.getById(id);
        return ResponseEntity.ok(budget);
    }
}